package com.yunke.util;

import com.yunke.entity.YunkeDazi;
import com.yunke.entity.YunkeDaziJoin;

/**
 * 云科搭子状态管理工具类
 * 
 * @author yunke
 * @since 2024-01-01
 */
public class YunkeDaziStatusUtil {

    /**
     * 搭子状态枚举
     */
    public enum DaziStatus {
        DRAFT(0, "草稿"),
        PUBLISHED(1, "已发布"),
        FINISHED(2, "已结束"),
        CANCELLED(3, "已取消"),
        DELETED(4, "已删除");
        
        private final Integer code;
        private final String desc;
        
        DaziStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDesc() {
            return desc;
        }
        
        public static DaziStatus fromCode(Integer code) {
            if (code == null) return null;
            for (DaziStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return null;
        }
    }

    /**
     * 搭子审核状态枚举
     */
    public enum AuditStatus {
        PENDING(0, "待审核"),
        APPROVED(1, "审核通过"),
        REJECTED(2, "审核拒绝");
        
        private final Integer code;
        private final String desc;
        
        AuditStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDesc() {
            return desc;
        }
        
        public static AuditStatus fromCode(Integer code) {
            if (code == null) return null;
            for (AuditStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return null;
        }
    }

    /**
     * 参与状态枚举
     */
    public enum JoinStatus {
        PENDING(0, "待审核"),
        APPROVED(1, "已通过"),
        REJECTED(2, "已拒绝"),
        QUIT(3, "已退出");
        
        private final Integer code;
        private final String desc;
        
        JoinStatus(Integer code, String desc) {
            this.code = code;
            this.desc = desc;
        }
        
        public Integer getCode() {
            return code;
        }
        
        public String getDesc() {
            return desc;
        }
        
        public static JoinStatus fromCode(Integer code) {
            if (code == null) return null;
            for (JoinStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return null;
        }
    }

    /**
     * 检查搭子是否可以编辑
     * 
     * @param dazi 搭子信息
     * @return 是否可以编辑
     */
    public static boolean canEdit(YunkeDazi dazi) {
        if (dazi == null) return false;
        
        DaziStatus status = DaziStatus.fromCode(dazi.getStatus());
        return status == DaziStatus.DRAFT || status == DaziStatus.PUBLISHED;
    }

    /**
     * 检查搭子是否可以删除
     * 
     * @param dazi 搭子信息
     * @param joinedCount 参与人数（除发布者外）
     * @return 是否可以删除
     */
    public static boolean canDelete(YunkeDazi dazi, long joinedCount) {
        if (dazi == null) return false;
        
        DaziStatus status = DaziStatus.fromCode(dazi.getStatus());
        // 只有草稿或没有其他人参与的搭子才能删除
        return status == DaziStatus.DRAFT || 
               (status == DaziStatus.PUBLISHED && joinedCount == 0);
    }

    /**
     * 检查搭子是否可以参与
     * 
     * @param dazi 搭子信息
     * @return 是否可以参与
     */
    public static boolean canJoin(YunkeDazi dazi) {
        if (dazi == null) return false;
        
        DaziStatus status = DaziStatus.fromCode(dazi.getStatus());
        AuditStatus auditStatus = AuditStatus.fromCode(dazi.getAuditStatus());
        
        // 必须是已发布且审核通过的搭子
        if (status != DaziStatus.PUBLISHED || auditStatus != AuditStatus.APPROVED) {
            return false;
        }
        
        // 检查人数限制
        if (dazi.getJoinedPeople() >= dazi.getNeedPeople()) {
            return false;
        }
        
        // 检查报名截止时间
        if (dazi.getDeadlineTime() != null && System.currentTimeMillis() > dazi.getDeadlineTime()) {
            return false;
        }
        
        // 检查活动时间
        if (System.currentTimeMillis() > dazi.getActivityTime()) {
            return false;
        }
        
        return true;
    }

    /**
     * 检查搭子是否可以置顶
     * 
     * @param dazi 搭子信息
     * @return 是否可以置顶
     */
    public static boolean canTop(YunkeDazi dazi) {
        if (dazi == null) return false;
        
        DaziStatus status = DaziStatus.fromCode(dazi.getStatus());
        AuditStatus auditStatus = AuditStatus.fromCode(dazi.getAuditStatus());
        
        return status == DaziStatus.PUBLISHED && auditStatus == AuditStatus.APPROVED;
    }

    /**
     * 检查参与记录是否可以退出
     * 
     * @param joinRecord 参与记录
     * @return 是否可以退出
     */
    public static boolean canQuit(YunkeDaziJoin joinRecord) {
        if (joinRecord == null) return false;
        
        JoinStatus status = JoinStatus.fromCode(joinRecord.getStatus());
        // 只有已通过的参与记录才能退出，且不能是发布者
        return status == JoinStatus.APPROVED && joinRecord.getIsPublisher() == 0;
    }

    /**
     * 检查搭子是否已过期
     * 
     * @param dazi 搭子信息
     * @return 是否已过期
     */
    public static boolean isExpired(YunkeDazi dazi) {
        if (dazi == null) return true;
        
        long currentTime = System.currentTimeMillis();
        
        // 活动时间已过
        if (currentTime > dazi.getActivityTime()) {
            return true;
        }
        
        // 报名截止时间已过
        if (dazi.getDeadlineTime() != null && currentTime > dazi.getDeadlineTime()) {
            return true;
        }
        
        return false;
    }

    /**
     * 检查搭子是否已满员
     * 
     * @param dazi 搭子信息
     * @return 是否已满员
     */
    public static boolean isFull(YunkeDazi dazi) {
        if (dazi == null) return true;
        return dazi.getJoinedPeople() >= dazi.getNeedPeople();
    }

    /**
     * 获取搭子状态描述
     * 
     * @param dazi 搭子信息
     * @return 状态描述
     */
    public static String getStatusDesc(YunkeDazi dazi) {
        if (dazi == null) return "未知";
        
        DaziStatus status = DaziStatus.fromCode(dazi.getStatus());
        if (status == null) return "未知";
        
        // 特殊状态判断
        if (status == DaziStatus.PUBLISHED) {
            if (isExpired(dazi)) {
                return "已过期";
            }
            if (isFull(dazi)) {
                return "已满员";
            }
            
            AuditStatus auditStatus = AuditStatus.fromCode(dazi.getAuditStatus());
            if (auditStatus == AuditStatus.PENDING) {
                return "待审核";
            }
            if (auditStatus == AuditStatus.REJECTED) {
                return "审核拒绝";
            }
        }
        
        return status.getDesc();
    }

    /**
     * 获取参与状态描述
     * 
     * @param joinRecord 参与记录
     * @return 状态描述
     */
    public static String getJoinStatusDesc(YunkeDaziJoin joinRecord) {
        if (joinRecord == null) return "未知";
        
        JoinStatus status = JoinStatus.fromCode(joinRecord.getStatus());
        return status != null ? status.getDesc() : "未知";
    }

    /**
     * 检查搭子置顶是否已过期
     * 
     * @param dazi 搭子信息
     * @return 是否已过期
     */
    public static boolean isTopExpired(YunkeDazi dazi) {
        if (dazi == null || dazi.getIsTop() != 1) return true;
        
        Long topEndTime = dazi.getTopEndTime();
        if (topEndTime == null) return true;
        
        return System.currentTimeMillis() > topEndTime;
    }
} 